package com.atguigu.juc.locks;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yfstart
 * @create 2022-06-30 15:49
 */
public class RnEntryLockDemo2 {
  static Lock lock = new ReentrantLock();

  public static void main(String[] args) {
      new Thread(()->{
          lock.lock();
          try {
            System.out.println(Thread.currentThread().getName()+"\t 外层调用");
              lock.lock();
              try {
                  System.out.println(Thread.currentThread().getName()+"\t 内层调用");
              } finally {
                  // 正常情况，加锁几次就要解锁几次
                  // 这里故意注释，实现加锁次数和释放次数不一样
                  // 由于加锁次数和释放次数不一样，第二个线程始终无法获取到锁，导致一直在等待。
                  lock.unlock();
              }
          } finally {
            lock.unlock();
          }
      },"t1").start();

      new Thread(()->{
          lock.lock();
          try {
              System.out.println(Thread.currentThread().getName()+"\t 开始运行");
          } finally {
              lock.unlock();
          }
      },"t2").start();
  }
}
